﻿using EFCoreRepository.Extensions;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RescueApp.Data;
using RescueApp.Models;
using Rescue.UI.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using Rescue.Services;

namespace RescueApp.UI
{
    public partial class DispatchUpdate : Form
    {
        private RescueContext db;
        private RescueApply rescueApply;
        private RescueDispatch rescueDispatch;
        private RescueApplyService srv;
        private RescueDispatchService srv2;

        public DispatchUpdate()
        {
            InitializeComponent();
        }

        public DispatchUpdate(RescueContext db, RescueDispatchService srv2, RescueDispatch rescueDispatch)
        {
            InitializeComponent();
            this.db = db;
            this.rescueDispatch = rescueDispatch;
            this.srv2 = srv2;
        }

        private bool ValidateItem()
        {
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("救援车辆司机不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("救援车辆车牌号不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox5.Text))
            {
                MessageBox.Show("到达现场时间不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!DateTime.TryParse(textBox5.Text, out _))
            {
                MessageBox.Show("到达现场时间格式不正确，请输入有效的日期时间格式（例如：yyyy-MM-dd HH:mm）！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (string.IsNullOrEmpty(textBox6.Text))
            {
                MessageBox.Show("故障原因不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (string.IsNullOrEmpty(textBox7.Text))
            {
                MessageBox.Show("救援费用不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }


            if (!float.TryParse(textBox7.Text, out _))
            {
                MessageBox.Show("救援费用格式不正确，请输入有效的数字！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;

        }

        //初始化时写入所选ID的信息
        private void DispatchUpdate_Load(object sender, EventArgs e)
        {
            textBox1.Text = rescueDispatch.DispatchID.ToString();
            textBox2.Text = rescueDispatch.RescueID.ToString();
            textBox3.Text = rescueDispatch.Driver;
            textBox4.Text = rescueDispatch.LicenseID;
            textBox5.Text = rescueDispatch.ArrivedTime.ToString();
            textBox6.Text = rescueDispatch.Cause;
            textBox7.Text = rescueDispatch.Cost.ToString();

            textBox1.ReadOnly = true;
            textBox1.BackColor = SystemColors.Control;
        }

        private void button1_Click(object sender, EventArgs e)//提交按钮
        {
            if (!ValidateItem()) return;
            rescueDispatch.RescueID = int.Parse(textBox2.Text.Trim());
            rescueDispatch.Driver = textBox3.Text.Trim();
            rescueDispatch.LicenseID = textBox4.Text.Trim();
            rescueDispatch.ArrivedTime = DateTime.Parse(textBox5.Text.Trim());
            rescueDispatch.Cause = textBox6.Text.Trim();
            rescueDispatch.Cost = float.Parse(textBox7.Text.Trim());
            var entity = db.RescueDispatches.FirstOrDefault(x => x.DispatchID == rescueDispatch.DispatchID);
            if (entity != null)
            {

                entity.RescueID = rescueDispatch.RescueID;
                entity.Driver = rescueDispatch.Driver;
                entity.LicenseID = rescueDispatch.LicenseID;
                entity.ArrivedTime = rescueDispatch.ArrivedTime;
                entity.Cause = rescueDispatch.Cause;
                entity.Cost = rescueDispatch.Cost;


                try
                {
                    srv2.Update(entity);
                    MessageBox.Show("修改成功!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)//取消按钮
        {
            textBox2.Text = rescueDispatch.RescueID.ToString();
            textBox3.Text = rescueDispatch.Driver;
            textBox4.Text = rescueDispatch.LicenseID;
            textBox5.Text = rescueDispatch.ArrivedTime.ToString();
            textBox6.Text = rescueDispatch.Cause;
            textBox7.Text = rescueDispatch.Cost.ToString();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)//派遣单ID
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            // 清空之前的错误提示
            label2.ForeColor = Color.Black;
            label2.Text = "救援申请ID";

            if (string.IsNullOrEmpty(textBox2.Text))
            {
                return; // 如果输入为空，不进行验证
            }

            int id;
            if (!int.TryParse(textBox2.Text, out id))
            {
                MessageBox.Show("救援申请ID格式不正确，请输入有效的数字！", "输入失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.ForeColor = Color.Red;

                return;
            }

            // 检查 RescueApply 是否存在
            if (!DbHelper.IsExistsWithORM(db, id))//检查RescueID是否存在
            {
                textBox2.ForeColor = Color.Red;
                MessageBox.Show("救援申请ID不存在，请输入一个有效的救援申请ID！", "输入失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            else
            {
                textBox2.ForeColor = Color.Green;

            }
        }
    }

}

